import {HwbaColor} from '../type';
import {Plugin} from '../extend';
import {parseHwba, rgbaToHwba, roundHwba} from '../color/model/hwb';
import {parseHwbaText, rgbaToHwbaText} from '../color/model/hwb-text';
import {Color} from '../color';

declare module '../color' {
    interface Color {
        /**
         * Converts a color to HWB (Hue-Whiteness-Blackness) color space and returns an object.
         * https://en.wikipedia.org/wiki/HWB_color_model
         */
        toHwb(): HwbaColor;

        /**
         * Converts a color to HWB (Hue-Whiteness-Blackness) color space and returns a string.
         * https://www.w3.org/TR/css-color-4/#the-hwb-notation
         */
        toHwbString(): string;
    }
}

/**
 * A plugin adding support for HWB (Hue-Whiteness-Blackness) color model.
 * https://en.wikipedia.org/wiki/HWB_color_model
 * https://www.w3.org/TR/css-color-4/#the-hwb-notation
 */
const hwbPlugin: Plugin = (ColorClass: typeof Color, parsers): void => {
    ColorClass.prototype.toHwb = function() {
        return roundHwba(rgbaToHwba(this.rgba));
    };

    ColorClass.prototype.toHwbString = function() {
        return rgbaToHwbaText(this.rgba);
    };

    parsers.string.push([parseHwbaText, 'hwb']);
    parsers.object.push([parseHwba, 'hwb']);
};

export default hwbPlugin;
